package main

import (
	"fmt"
	"log"
	"test/bolt"
)

func BoltTest() {

	db, err := bolt.Open("bolt01.db", 0600, nil)
	if err != nil {
		log.Panic("打开数据库失败")
		return
	}
	defer db.Close()
	err = db.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket([]byte("b1"))
		if bucket == nil {
			//log.Panic("没有bucket")
			bucket, err = tx.CreateBucket([]byte("b1"))
			if err != nil {
				log.Panic("创建失败")
			}
			bucket.Put([]byte("0001"), []byte("01"))
			bucket.Put([]byte("0002"), []byte("02"))
		}
		return nil
	})

	err = db.View(func(tx *bolt.Tx) error {
		bucket := tx.Bucket([]byte("b1"))
		if bucket == nil {
			log.Panic("缺少bucket：err")
		}
		v1 := bucket.Get([]byte("0001"))
		v2 := bucket.Get([]byte("0001"))
		fmt.Printf("v1:%x\nv2:%x\n", v1, v2)
		return nil
	})
	return
}
func main() {
	BoltTest()
}
